package project.messagestack.GUI;

import java.net.URI;
import java.util.ArrayList;
import project.messagestack.R;
import project.messagestack.utils.DatabaseManager;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MailList extends Activity {
  public static final String TAG = "MailList";
  private Button mAddMailButton;
  private ListView mMailList;
  private ArrayList<MailAcount> list;
  private final int NEW_ACOUNT = 100;
  private final int EDIT_ACOUNT = 150;
  private DatabaseManager dm;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mail_list);
    mAddMailButton = (Button) findViewById(R.id.addMailButton);
    dm = new DatabaseManager(this);
    mMailList = (ListView) findViewById(R.id.mailList);
    list = new ArrayList<MailAcount>();
    populateList();
    mMailList.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
          long arg3) {
        Intent i = new Intent(getApplicationContext(), MailAdder.class);
        i.putExtra("login", list.get(arg2).getLogin());
        i.putExtra("pass", list.get(arg2).getPassword());
        i.putExtra("serwer", list.get(arg2).getSerwer());
        i.putExtra("port", Integer.toString(list.get(arg2).getPort()));
        i.putExtra("index", arg2);
        i.putExtra("type", list.get(arg2).getType());
        startActivityForResult(i, EDIT_ACOUNT);

      }

    });
    mAddMailButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        Log.d(TAG, "mAddMailButton clicked");
        launchMailAdder();
      }
    });
  }

  public void launchMailAdder() {
    Intent i = new Intent(getApplicationContext(), MailAdder.class);
    startActivityForResult(i, NEW_ACOUNT);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
      switch (requestCode) {
      case NEW_ACOUNT:
        MailAcount a = new MailAcount(data.getStringExtra("login"),
            data.getStringExtra("pass"), data.getStringExtra("serwer"),
            data.getIntExtra("port", 0), data.getStringExtra("type"));
        list.add(a);
        URI serviceURL = URI.create(a.getLogin() + ":" + a.getPassword() + ":"
            + a.getSerwer() + ":" + a.getPort()+":"+a.getType());
        dm.updateConfiguration("MAIL", serviceURL, 0);
        break;
      case EDIT_ACOUNT:
        int index = data.getIntExtra("index", -1);
        if (index >= 0) {
          list.get(index).setLogin(data.getStringExtra("login"));
          list.get(index).setPassword(data.getStringExtra("pass"));
          list.get(index).setSerwer(data.getStringExtra("serwer"));
          list.get(index).setPort(data.getIntExtra("port", 0));
          list.get(index).setType(data.getStringExtra("type"));
          a = new MailAcount(data.getStringExtra("login"),
              data.getStringExtra("pass"), data.getStringExtra("serwer"),
              data.getIntExtra("port", 0), data.getStringExtra("type"));
          serviceURL = URI.create(a.getLogin() + ":" + a.getPassword() + ":"
              + a.getSerwer() + ":" + a.getPort()+":"+a.getType());
          dm.updateConfiguration("MAIL", serviceURL, 0);
        }

        break;
      }
      populateList();
    }
  }

  @SuppressWarnings({ "unchecked", "rawtypes" })
  protected void populateList() {
    list.clear();
    Cursor a = dm.getServicesConfiguration();
    a.moveToFirst();
    while (!a.isAfterLast()) {
      if (a.getString(1).startsWith("MAIL")) {
        String temp = a.getString(2);
        String content[] = temp.split(":");
        MailAcount acount = new MailAcount(content[0], content[1], content[2],
            Integer.parseInt(content[3]), content[4]);
        list.add(acount);
      }
      a.moveToNext();
    }
    String[] lista = new String[list.size()];
    for (int i = 0; i < list.size(); i++) {
      lista[i] = list.get(i).getLogin() + "@" + list.get(i).getSerwer();
    }
    mMailList.setAdapter(new ArrayAdapter(this,
        android.R.layout.simple_list_item_1, lista));
  }
}
